<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 6/4/21
 * Time: 12:13 AM
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use  app\api\model\UserMoney as UserMoneyModel;
use app\api\service\order\ProductOrderSer;
use app\api\service\Token as TokenService;
use app\api\model\ProductOrder as ProductOrderModel;

class UserMoney  extends BaseController
{
    public function ceshi()
    {
        $order = ProductOrderModel::get(71);
        $orderSer = new ProductOrderSer($order);
        $orderSer->changeStatus();
    }
    /**
     * 获取用户余额变化
     */
    public function getDataList($time)
    {
        if($time) {
            $date = date('Y-m',$time/1000);
        } else {
            $date = date('Y-m');
        }
        $timebegin = strtotime($date) ; //开始时间戳
        $day = date('t',$timebegin);
        $timeend = $timebegin + 86400 * ($day  - 1); //结束时间戳
        $where[] = ['create_time','elt',$timeend];
        $where[] = ['create_time','egt',$timebegin];
        $uid = TokenService::getCurrentUid();
        $where[] = ['user_id','eq',$uid];
        $data['date'] = $date;
        $whereOne = $where;
        $whereOne[] = ['type','eq',2];
        $data['min_money'] = UserMoneyModel::where($whereOne)->sum('money');
        $whereTwo = $where;
        $whereTwo[] = ['type','eq',1];
        $data['max_money'] = UserMoneyModel::where($whereTwo)->sum('money');
        $data['user_money'] = $data['max_money'] - $data['min_money'];
        $data['data'] = UserMoneyModel::where($where)->with('order')->order('id desc')->select()->toArray();
        return $data;
    }

    /**
     * 获取奖励列表
     */
    public function getUserMoneyList()
    {
        $uid = TokenService::getCurrentUid();
        $where[] = ['type', 'eq', 1];
        $where[] = ['user_id','eq',$uid];
        return UserMoneyModel::where($where)->with('order')->order('id desc')->select()->toArray();
    }
}